package org.ns.summer.core;


import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class MethodUtils {


    public  static List<Method> findMethodByAnnotation(Class<?> beanType, Class<?> parameterAnnotationtype) {
        ArrayList<Method> result = new ArrayList<>();
        Method[] declaredMethods = beanType.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            if (AnnotationUtils.existAnnotation(declaredMethod, parameterAnnotationtype)) {
                result.add(declaredMethod);
            }
        }

        return result;
    }
    public  static boolean existMethodByAnnotation(Class<?> beanType, Class<?> parameterAnnotationtype) {
        Method[] declaredMethods = beanType.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            if (AnnotationUtils.existAnnotation(declaredMethod, parameterAnnotationtype)) {
                return true;
            }
        }

        return false;
    }


    public static String toMethodString(Method method) {
        StringBuilder stringBuilder = new StringBuilder(100);
        stringBuilder.append(method.getName()).append("(");
        Class<?>[] parameterTypes = method.getParameterTypes();
        for (Class<?> parameterType : parameterTypes) {
            stringBuilder.append(parameterType.getName()).append(",");
        }
        if (parameterTypes.length > 0) {
            stringBuilder.replace(stringBuilder.length() - 1, stringBuilder.length(), ")");
        } else {
            stringBuilder.append(")");
        }
        return stringBuilder.toString();
    }
}
